<?php
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 后台模板公共方法
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\controller\Adminbase;
use think\Db;

class Common extends Adminbase {
    public function add_edit() {
        $data = $this->request->param();

        $module    = $data['module'] ?: request()->module();
        $model     = $data['model'];
        $is_verify = $data['is_verify'];

        if ($module == 'app') {
            $module = '';
        }

        if ($is_verify == 1) {
            /*
             * 校验数据
             * 1、is_scene = 1表示使用场景验证
             */
            if (!empty($data['is_scene']) && $data['is_scene'] == 1) {
                if ($data['action'] == 1) {
                    $validate_model = 'app\\' . $module . '\\validate' . '\\' . $model . '.add';
                } else {
                    $validate_model = 'app\\' . $module . '\\validate' . '\\' . $model . '.edit';
                }
            } else {
                $validate_model = 'app\\' . $module . '\\validate' . '\\' . $model;
            }

            if (empty($module)) {
                $validate_model = 'app\\' . 'validate' . '\\' . $model;
            }

            $result = $this->validate($data, $validate_model);
            if (true !== $result) {
//            $this->error($result);
                return json(['code' => -1, 'msg' => $result]);
            }
        }

//        //处理内容图片
//        if (isset($data['content']) || !empty($data['content'])) {
//            $content = $data['content'];
//            //如果查找到图片
//            if (strpos($data['content'], '<img') !== false) {
        /*                $preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';//匹配img标签的正则表达式*/
//                preg_match_all($preg, $content, $images);//这里匹配所有的img
//                foreach ($images[1] as $key => $val) {
//                    //如果图片没有域名处理
//                    if (strpos($val, '://') === false) {
////                    $img_arr = explode('uploads', $val);
//                        $content = str_replace($val, 'http://' . $_SERVER['HTTP_HOST'] . $val, $content);
//                    }
//                }
//                $data['content'] = $content;
//            }
//        }

        //action=1表示新增数据，action=2表示编辑数据, 使用model进行数据处理再新增/编辑
        //覆盖model值，避免默认使用common目录model
        if ($module) {
            $model = 'app\\' . $module . '\\model' . '\\' . $model;
        } else {
            $model = 'app\\' . $module . 'model' . '\\' . $model;
        }

        if ($data['action'] == 1) {
            $res = saveDataByModel($model, $data);
        } else {
            $res = saveDataByModel($model, $data, ['id' => $data['id']]);
        }
        if ($res) {
            $result = ['code' => 0, 'msg' => '操作成功'];
        } else {
            $result = ['code' => -1, 'msg' => '操作失败'];
        }
        return json($result);

    }

    /**
     * 删除单条数据
     */
    public function delete() {
        $id = $this->request->param('id/d');
        empty($id) && $this->error('参数不能为空！');
        $module = $this->request->param('module') ?: request()->module();

        $model = $this->request->param('model');
        if (empty($model)) {
            $this->error('模型不能为空！');
        }

        //覆盖model值，避免默认使用common目录model
        if ($module == 'app') {
            $model = 'app\\' . 'model' . '\\' . $model;
        } else {
            $model = 'app\\' . $module . '\\model' . '\\' . $model;
        }

        $res = model($model)::destroy($id);
        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    /**
     * 删除多条数据
     */
    public function delAll() {
        $ids    = $this->request->param('ids/a', null);
        $module = $this->request->param('module') ?: request()->module();

        $model = $this->request->param('model');
        if (empty($model)) {
            $this->error('模型不能为空！');
        }

        //覆盖model值，避免默认使用common目录model
        if ($module == 'app') {
            $model = 'app\\' . 'model' . '\\' . $model;
        } else {
            $model = 'app\\' . $module . '\\model' . '\\' . $model;
        }
//        $model = 'app\\' . $module . '\\model' . '\\' . $model;


        $res = model($model)::destroy($ids);
        if ($res) {
            $result = ['code' => 0, 'msg' => '删除成功'];
        } else {
            $result = ['code' => -1, 'msg' => '删除失败'];
        }
        return json($result);
    }

    /**
     * 修改状态
     */
    public function setState() {
        $id = $this->request->param('id/d');
        empty($id) && $this->error('参数不能为空！');

        //数值
        $status = $this->request->param('status/d');
        //模型
        $model = $this->request->param('model');
        //字段名
        $field = $this->request->param('field');
        if (empty($model)) {
            $this->error('模型不能为空！');
        }
        $res = Db::name($model)->where('id', $id)->update([$field => $status]);
        if ($res) {
            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    /**
     * 表格字段编辑修改
     */
    public function fieldEdit() {
        //数据ID
        $id = $this->request->param('id/d');
        //模型
        $model = $this->request->param('model');
        //字段名
        $field = $this->request->param('field');
        //数值
        $value = $this->request->param('value');
        if (empty($model)) {
            $this->error('模型不能为空！');
        }

        $res = Db::name($model)->where('id', $id)->update([$field => $value]);
        if ($res) {
            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }

    }

}
